package view.guitare;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JPanel;

import model.notes.Note;
import view.notes.NoteDecompositionJFrame;

public class NeckPanel extends JPanel implements MouseListener {

	public static int TOP_LEFT_X = 20;
	public static int TOP_LEFT_Y = 20;
	public static int TOP_RIGHT_X = NoteDecompositionJFrame.DIMENSION.width - 40;
	public static int TOP_RIGHT_Y = 20;

	public static int STRINGS_DELTA_Y = 30;
	public static int FRETS_DELTA_X = 50;

	private final Map<String, NoteShapeImpl> noteShapes = new HashMap<String, NoteShapeImpl>();
	private final InformationPanel informationPanel;
	
	
	public NeckPanel(InformationPanel informationPanel) {
		addMouseListener(this);
		this.informationPanel = informationPanel;
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		for (int i = 0; i < 6; i++) {
			g2d.drawLine(TOP_LEFT_X, TOP_LEFT_Y + i * STRINGS_DELTA_Y,
					TOP_RIGHT_X, TOP_RIGHT_Y + i * STRINGS_DELTA_Y);
		}
		for (int i = 0; i < 19; i++) {
			g2d.drawLine(TOP_LEFT_X + i * FRETS_DELTA_X, TOP_LEFT_Y, TOP_LEFT_X
					+ i * FRETS_DELTA_X, TOP_LEFT_Y + 5 * STRINGS_DELTA_Y);
		}
		g2d.fillRect(TOP_LEFT_X - 10, TOP_LEFT_Y, 10, 5 * STRINGS_DELTA_Y + 1);

		// String 1
		drawNote(g2d, 1, 1, Note.FA);
		drawNote(g2d, 1, 3, Note.SOL);
		drawNote(g2d, 1, 5, Note.LA);
		drawNote(g2d, 1, 7, Note.SI);
		drawNote(g2d, 1, 8, Note.DO);
		drawNote(g2d, 1, 10, Note.RE);
		drawNote(g2d, 1, 12, Note.MI);

		// String 2
		drawNote(g2d, 2, 1, Note.DO);
		drawNote(g2d, 2, 3, Note.RE);
		drawNote(g2d, 2, 5, Note.MI);
		drawNote(g2d, 2, 6, Note.FA);
		drawNote(g2d, 2, 8, Note.SOL);
		drawNote(g2d, 2, 10, Note.LA);
		drawNote(g2d, 2, 12, Note.SI);
		
		// String 3
		drawNote(g2d, 3, 2, Note.LA);
		drawNote(g2d, 3, 4, Note.SI);
		drawNote(g2d, 3, 5, Note.DO);
		drawNote(g2d, 3, 7, Note.RE);
		drawNote(g2d, 3, 9, Note.MI);
		drawNote(g2d, 3, 10, Note.FA);
		drawNote(g2d, 3, 12, Note.SOL);
		
		// String 4
		drawNote(g2d, 4, 2, Note.MI);
		drawNote(g2d, 4, 3, Note.FA);
		drawNote(g2d, 4, 5, Note.SOL);
		drawNote(g2d, 4, 7, Note.LA);
		drawNote(g2d, 4, 9, Note.SI);
		drawNote(g2d, 4, 10, Note.DO);
		drawNote(g2d, 4, 12, Note.RE);
		
		// String 5
		drawNote(g2d, 5, 2, Note.SI);
		drawNote(g2d, 5, 3, Note.DO);
		drawNote(g2d, 5, 5, Note.RE);
		drawNote(g2d, 5, 7, Note.MI);
		drawNote(g2d, 5, 8, Note.FA);
		drawNote(g2d, 5, 10, Note.SOL);
		drawNote(g2d, 5, 12, Note.LA);
		
		
		// String 5
		drawNote(g2d, 6, 1, Note.FA);
		drawNote(g2d, 6, 3, Note.SOL);
		drawNote(g2d, 6, 5, Note.LA);
		drawNote(g2d, 6, 7, Note.SI);
		drawNote(g2d, 6, 8, Note.DO);
		drawNote(g2d, 6, 10, Note.RE);
		drawNote(g2d, 6, 12, Note.MI);
	}

	private void drawNote(Graphics2D g2d, int string, int fret, Note note) {
		NoteShapeImpl noteShape = new NoteShapeImpl(string, fret, note);
		this.noteShapes.put(noteShape.getKey(), noteShape);
		g2d.draw(noteShape);
		g2d.setColor(Color.BLUE);
		g2d.fill(noteShape);

	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		if ((e.getButton() == 1)) {
			for (NoteShapeImpl noteShape : this.noteShapes.values()) {
				if (noteShape.contains(e.getX(), e.getY())) {
					Graphics2D g2d = (Graphics2D) getGraphics();
					g2d.setColor(Color.RED);
					g2d.fill(noteShape);
					this.informationPanel.setNoteLabel(noteShape.getNote().toString());
				}
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if ((e.getButton() == 1)) {
			for (NoteShapeImpl noteShape : this.noteShapes.values()) {
				if (noteShape.contains(e.getX(), e.getY())) {
					Graphics2D g2d = (Graphics2D) getGraphics();
					g2d.setColor(Color.BLUE);
					g2d.fill(noteShape);
					this.informationPanel.eraseNoteLabel();
					g2d.setColor(Color.BLACK);
					g2d.drawString(noteShape.getNote().getInternationalName().toString(), (int) noteShape.getCenterX() - NoteShapeImpl.CIRCLE_RADIUS/4, (int) noteShape.getCenterY() + NoteShapeImpl.CIRCLE_RADIUS/4);
				}
			}
		}
	}
}
